import React, { useEffect, useState } from "react";
import { Space, Table, Card, Switch, Modal, Button, Drawer } from "antd";
import type { ColumnsType } from "antd/es/table";
import { useSelector } from "react-redux";
import {
	cancelTopCircleFriends,
	getCircleFriendDetails,
	getCircleFriendLists,
	removeCircleFriend,
	topCircleFriends,
	updateCircleFriend
} from "@/redux/modules/circleFriend/action";
import { RootState } from "@/redux";
import useModal from "@/hooks/useModal";
import useMessage from "@/hooks/useMessage";
import CircleFriendDetail from "../CircleFriendDetail";
import styles from "./index.module.less";
import CircleFriendAdd from "../CircleFriendAdd";
import CircleFriendEdit from "../CircleFriendEdit";
import usePagination from "@/hooks/usePagination";
import CircleFriendFilter from "../CircleFriendFilter";
import { CircleFriend } from "@/api/interface/circleFriend";

const CircleFriendList: React.FC = props => {
	interface DataType {
		id: number;
		key: number;
		user: string;
		top: boolean;
		visible: boolean;
		content: string;
		createdAt: string;
		updatedAt: string;
		dianzanCount: number;
	}
	[];

	const columns: ColumnsType<DataType> = [
		{
			title: "内容",
			dataIndex: "content",
			key: "content",
			ellipsis: true
		},
		{
			title: "发布人",
			dataIndex: "user",
			key: "user"
		},
		{
			title: "是否置顶",
			dataIndex: "top",
			key: "top",
			render: (_, row) => <Switch defaultChecked={row.top} onChange={(checked: boolean) => onCircleFriendTop(checked, row.id)} />
		},
		{
			title: "是否隐藏",
			dataIndex: "visible",
			key: "visible",
			render: (_, row) => (
				<Switch
					defaultChecked={row.visible}
					onChange={(checked: boolean) => updateCircleFriend({ id: row.id, visible: Number(checked) })}
				/>
			)
		},
		{
			title: "点赞数",
			dataIndex: "dianzanCount",
			key: "dianzanCount"
		},
		{
			title: "创建日期",
			dataIndex: "createdAt",
			key: "createdAt"
		},
		{
			title: "修改日期",
			dataIndex: "updatedAt",
			key: "updatedAt"
		},
		{
			title: "操作",
			key: "action",
			render: (_, row) => {
				return (
					<>
						<Space size="middle">
							<a onClick={() => openDetail(row.id)}>详情</a>
							<a onClick={() => openEdit(row.id)}>修改</a>
							<a onClick={() => openModal(row.id)}>删除</a>
						</Space>
					</>
				);
			}
		}
	];

	const listExtra = (
		<div style={{ display: "flex", gap: 8 + "px" }}>
			<Button type="primary" onClick={() => setAddVisible(true)}>
				新增
			</Button>
			<Button type="primary" onClick={() => getCircleFriendLists()}>
				刷新
			</Button>
		</div>
	);

	const { contextHolder, success } = useMessage();
	const { id, isModalOpen, openModal, closeModal } = useModal();

	const deleteCircleFriend = () => {
		removeCircleFriend(id);
		closeModal();
		success("删除成功");
	};
	const onCircleFriendTop = (checked: boolean, id: number) => {
		if (checked) {
			// 置顶
			topCircleFriends(id);
		} else {
			cancelTopCircleFriends(id);
		}
	};

	const circleFriendList = useSelector((state: RootState) => state.circleFriend.circleFriendList);
	const circleFriendTotal = useSelector((state: RootState) => state.circleFriend.circleFriendTotal);

	const data: DataType[] = circleFriendList.map(item => {
		return {
			id: item.id,
			key: item.id,
			user: item.user.name,
			top: item.top === 1 ? true : false,
			visible: item.visible === 1 ? true : false,
			content: item.content,
			createdAt: item.createdAt,
			updatedAt: item.updatedAt,
			dianzanCount: item.dianzanCount
		};
	});

	useEffect(() => {
		getCircleFriendLists();
	}, []);

	// 详情
	const [detailVisible, setDetailVisible] = useState(false);
	const openDetail = (id: number) => {
		// 获取详情
		getCircleFriendDetails(id);
		setDetailVisible(true);
	};

	// 修改
	const [editVisible, setEditVisible] = useState(false);
	const openEdit = async (id: number) => {
		getCircleFriendDetails(id);
		setEditVisible(true);
	};
	const onCloseEdit = () => {
		getCircleFriendLists();
		setEditVisible(false);
	};

	// 新增
	const [addVisible, setAddVisible] = useState(false);
	const onCloseAdd = () => {
		getCircleFriendLists();
		setAddVisible(false);
	};

	const { onChangePage } = usePagination(getCircleFriendLists);

	// 搜索
	const onSearch = (circleFriend?: CircleFriend) => {
		getCircleFriendLists(1, 10, circleFriend);
	};

	return (
		<div className={styles.root}>
			{contextHolder}
			<CircleFriendFilter onSearch={onSearch} />
			<Card title="数据列表" extra={listExtra}>
				<Table columns={columns} dataSource={data} pagination={{ total: circleFriendTotal, onChange: onChangePage }} />
			</Card>

			<Modal title="提醒" mask={false} visible={isModalOpen} onOk={deleteCircleFriend} onCancel={closeModal}>
				确定删除该朋友圈吗
			</Modal>

			<Drawer title="详情" placement="right" width={500} onClose={() => setDetailVisible(false)} visible={detailVisible}>
				<CircleFriendDetail></CircleFriendDetail>
			</Drawer>

			<Drawer
				title="新增"
				placement="right"
				width={500}
				onClose={() => {
					setAddVisible(false);
				}}
				visible={addVisible}
			>
				<CircleFriendAdd onComfirm={onCloseAdd}></CircleFriendAdd>
			</Drawer>

			<Drawer
				title="修改"
				placement="right"
				width={500}
				onClose={() => {
					setEditVisible(false);
				}}
				visible={editVisible}
			>
				<CircleFriendEdit onComfirm={onCloseEdit}></CircleFriendEdit>
			</Drawer>
		</div>
	);
};

export default CircleFriendList;
